//
//  PaidNumViewController.m
//  联付商圈
//
//  Created by 韩佳睿 on 2017/8/15.
//  Copyright © 2017年 lianfu. All rights reserved.
//

#import "PaidNumViewController.h"

@interface PaidNumViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *surePaywordTextField;

@end

@implementation PaidNumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self prepareNavBar];
}

- (void)prepareNavBar{
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"重置支付密码";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}

- (IBAction)getCodeAction:(id)sender {
    if (![NSString isNilOrEmpty:_phoneNumberTextField.text]) {
        NSDictionary *param = @{@"mobile" : _phoneNumberTextField.text};
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
        
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.securityPolicy.allowInvalidCertificates = YES;
        
        [manager GET:@"http://www.beidikeji.com/life/public/jiekou/sendsms?" parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            if (dic[@"success"]) {
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = dic[@"success"];
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
            } else {
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = dic[@"error"];
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        }];
    } else {
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
        HUD.labelText = @"请输入手机号码";
        HUD.mode = MBProgressHUDModeText;
        [HUD hide:YES afterDelay:1.0];
    }
}

- (IBAction)finishiAction:(id)sender {
    if (![self isNil]) {
        NSDictionary *param = @{@"name" :  _phoneNumberTextField.text, @"payword" : _surePaywordTextField.text, @"key" : _codeTextField.text};
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.securityPolicy.allowInvalidCertificates = YES;
        
        [manager GET:[Host stringByAppendingString:@"changepayword?"] parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            if (dic[@"success"]) {
                
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = dic[@"success"][@"message"];
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = dic[@"error"][@"message"];
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        }];
    } else {
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
        HUD.labelText = @"请填写完整信息";
        HUD.mode = MBProgressHUDModeText;
        [HUD hide:YES afterDelay:1.0];
    }
}

- (BOOL)isNil{
    return [NSString isNilOrEmpty:_phoneNumberTextField.text] && [NSString isNilOrEmpty:_codeTextField.text] && [NSString isNilOrEmpty:_surePaywordTextField.text];
}

@end
